package com.minimoment.web.interceptor;

import com.minimoment.model.domain.User;
import com.minimoment.model.service.UserService;
import com.minimoment.util.common.CommomUtils;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.SessionAware;
import org.springframework.beans.factory.annotation.Autowired;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.xml.ws.RequestWrapper;
import java.util.List;

/**
 * Created by etund on 2014/11/1.
 */
public class AutoLoginItc extends AbstractInterceptor {
    @Resource
    private UserService userService;
    @Override
    public String intercept(ActionInvocation actionInvocation) throws Exception {
        HttpServletRequest request = ServletActionContext.getRequest();
        String[] loginInfo = CommomUtils.getCookie(request,"loginInfo");
        if(loginInfo!=null&&loginInfo.length>2) {
            User user = new User();
            user.setLoginName(loginInfo[0]);
            user.setPassword(loginInfo[1]);
            List<User> users =  this.userService.getByCondition(user);
            user = users.get(0);
            if(user!=null){
                HttpSession session = ServletActionContext.getRequest().getSession();
                session.setAttribute("user",user);
                System.out.println(session+"========session+user========="+user);
                return  actionInvocation.invoke();
            }
        }
        return  actionInvocation.invoke();
    }

}
